import { isString,isNumber } from "../../js/is";

export const formatDate =(date:any|Date):string=> {
    let dat = new Date(date);
    let YY = dat.getFullYear() + '-';
    let MM = (dat.getMonth() + 1 < 10 ? '0' + (dat.getMonth() + 1) : dat.getMonth() + 1) + '-';
    let DD = (dat.getDate() < 10 ? '0' + (dat.getDate()) : dat.getDate());
    let hh = (dat.getHours() < 10 ? '0' + dat.getHours() : dat.getHours()) + ':';
    let mm = (dat.getMinutes() < 10 ? '0' + dat.getMinutes() : dat.getMinutes()) + ':';
    let ss = (dat.getSeconds() < 10 ? '0' + dat.getSeconds() : dat.getSeconds());
    return YY + MM + DD +" "+hh + mm + ss;
  }

/**
 * @public 获取指定10位,13位时间戳 对应的 季度信息
 */
export const getSeasonInfoByTimeStamp = (timestamp:any): Record<string,any>|undefined => {
    const seasonDataArr = [
        {
            startDateMonth:1,
            startDateDay:1,
            endDateMonth:3,
            endDateDay:31,
            label:"第一季度",
            value:0
        },
        {
            startDateMonth:4,
            startDateDay:1,
            endDateMonth:6,
            endDateDay:30,
            label:"第二季度",
            value:1
        },
        {
            startDateMonth:7,
            startDateDay:1,
            endDateMonth:9,
            endDateDay:30,
            label:"第三季度",
            value:3
        },
        {
            startDateMonth:10,
            startDateDay:1,
            endDateMonth:12,
            endDateDay:30,
            label:"第四季度",
            value:3
        }
    ];
    if(!timestamp||!isString(timestamp)&&!isNumber(timestamp)) return;
    timestamp = timestamp.toString().trim();
    const _len = timestamp.length;
    if(_len != 10&&_len != 13) return;
    const _date = _len==10?new Date(parseInt(timestamp) * 1000):new Date(parseInt(timestamp));
    let _m:number = _date.getMonth() + 1;
    for(let i =0;i<seasonDataArr.length;i++){
        let season = seasonDataArr[i];
        if(_m>=season.startDateMonth && _m<=season.endDateMonth){
            return season;
        }
    }
}

/**
 * @public 获取本周的 周一和周日的时间戳
 */
export const getThisWeekStartAndEnd = ():Record<any,number|string>=>{
    const now = new Date();
    const nowTime = now.getTime() ;
    const day = now.getDay()||7;
    const oneDayTime = 24*60*60*1000 ;
    const _mon = nowTime - (day-1)*oneDayTime ;//显示周一
    const _sun =  nowTime + (7-day)*oneDayTime ;//显示周日
    return{monday:_mon,mondayText:formatDate(_mon),sunday:_sun,sundayText:formatDate(_sun)}
}

/**
 * @public YYYY-MM-dd hh:mm:ss 转年-月-日 
 */
export const getCommonTimeInChinese =(str:string):string =>{
    if(str){
        let left = str.split(" ")[0];
        let tarr = left.split("-");
        let timespan = tarr[0]+'年'+tarr[1]+"月"+tarr[2]+'日';
        return timespan;
    }else{
        return "";
    }
}

/**
 * @public YYYY-MM-dd hh:mm:ss 转年-月-日 
 */
export const getCommonTimeInChineseWithSpe =(startTime:string,endTime:string,spc:string):string =>{
    let sp:string = " 至 ";
    if(spc){ sp = spc };
    let startTimeSpan:string = "";
    let endTimeSpan:string = "";
    let result:string = "";
    if(startTime){
        startTimeSpan = getCommonTimeInChinese(startTime);
    }
    if(endTime){
        endTimeSpan = getCommonTimeInChinese(endTime);
    }
    result = startTimeSpan+sp+endTimeSpan;
    return result;
}

